package com.rogers.rmcdouga.skirmishgadget.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class GridDialog extends DialogBox
{
	private static final String GRID_TITLE = "Set Grid Properties";
	
	private static GridDialogUiBinder uiBinder = GWT.create(GridDialogUiBinder.class);

	interface GridDialogUiBinder extends UiBinder<Widget, GridDialog>
	{
	}

	interface DialogResult {
		void action(GridDialog dlg);
	}
	
	// State variables

	// member variables
	private DialogResult okAction;
	private DialogResult cancelAction;

	@UiField
	TextBox screenGridSize;

	@UiField
	TextBox mapGridSize;

	@UiField
	Button okButton;

	@UiField
	Button cancelButton;
	
	public GridDialog() {
		super();
		setText(GRID_TITLE);
		setWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	  protected void onPreviewNativeEvent(NativePreviewEvent preview) {
	    super.onPreviewNativeEvent(preview);

	    NativeEvent evt = preview.getNativeEvent();
	    if (evt.getType().equals("keydown")) {
	      // Use the popup's key preview hooks to close the dialog when either
	      // enter or escape is pressed.
	      switch (evt.getKeyCode()) {
	        case KeyCodes.KEY_ENTER:
	        case KeyCodes.KEY_ESCAPE:
	          hide();
	          break;
	      }
	    }
	  }

	@UiHandler("okButton")
	void okButtonClick(ClickEvent e)
	{
		if (getOkAction() != null)
			getOkAction().action(this);
		hide();
	}
	
	@UiHandler("cancelButton")
	void cancelButtonClick(ClickEvent e)
	{
    	if (getCancelAction() != null)
			getCancelAction().action(this);
		hide();
	}

	public DialogResult getOkAction()
	{
		return okAction;
	}

	public void setOkAction(DialogResult okAction)
	{
		this.okAction = okAction;
	}

	public DialogResult getCancelAction()
	{
		return cancelAction;
	}

	public void setCancelAction(DialogResult cancelAction)
	{
		this.cancelAction = cancelAction;
	}

	public int getScreenGridSize()
	{
		return Integer.parseInt(screenGridSize.getText());
	}

	public void setScreenGridSize(int screenGridSize)
	{
		this.screenGridSize.setText(Integer.toString(screenGridSize));
	}

	public int getMapGridSize()
	{
		return Integer.parseInt(mapGridSize.getText());
	}

	public void setMapGridSize(int mapGridSize)
	{
		this.mapGridSize.setText(Integer.toString(mapGridSize));
	}
	
	
}
